/**
 * Created by feifan on 2016/6/24.
 */
var express = require('express');
var router = express.Router();

router.post('/:page', (req, res)=> {
    db.select('articles', (item)=> {
        if (!req.body.category||req.body.category=='all') return true;
        return item.category && item.category.indexOf(req.body.category) > -1;
    }, (data)=> {
        let articles = data.filter((item)=> {
            if (!req.body.tags || req.body.tags.length == 0) return true;
            //log(item,req.body.tags)
            return isContained(item.tag,req.body.tags);
        });
        let page = req.params.page || 1;
        let result = {
            article: articles.reverse().slice((page - 1) * CONF.page, (page - 1) * CONF.page + CONF.page),
            //maxPage: 10
            maxPage: Math.ceil(articles.length / CONF.page)
        };
        //log(result)
        res.json(result);
    })

});

module.exports = router;